//
//  QYGDollDetailView.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2017/12/28.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGDollDetailView.h"

@interface QYGDollDetailView()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation QYGDollDetailView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        
        [self viewWithcornerRadius:8 borderWidth:0 borderColor:nil backgroundColor:[UIColor whiteColor]];
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageView];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
//    self.imageView.frame = self.bounds;
}

- (void)setDetail_img:(NSURL *)detail_img{
    _detail_img = detail_img;
    
    if(NULLString(detail_img.absoluteString)) return;
    // 下载图片
    
//    return;
    WS(weakSelf);
    [self.imageView setImageWithURL:detail_img placeholder:nil options:YYWebImageOptionShowNetworkActivity completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        // 图片尺寸
        CGFloat pictureW = weakSelf.width;
        CGFloat pictureH = !image ? 0 : pictureW * image.size.height / image.size.width;
        if (pictureH > weakSelf.height) { // 图片显示高度超过一个屏幕, 需要滚动查看
            weakSelf.imageView.frame = CGRectMake(0, 0, pictureW, pictureH);
            weakSelf.scrollView.contentSize = CGSizeMake(0, pictureH);
        } else {
            weakSelf.imageView.size = CGSizeMake(pictureW, pictureH);
            weakSelf.imageView.centerY = weakSelf.height * 0.5;
        }
        
        //回调或者说是通知主线程刷新，
        weakSelf.imageView.image = image;
        
    }];
    
   
}
- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}
- (UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] init];
    }
    return _scrollView;
}
@end
